//
//  WKWalletManagerViewController.m
//  ourToken
//
//  Created by wakeup on 2018/5/31.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKWalletManagerViewController.h"
#import "WKBaseHeader.h"

@interface WKWalletManagerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;//控制器下面的view
@property (nonatomic, strong) UIButton *foundWalletBtn;//创建钱包按钮
@property (nonatomic, strong) UIButton *loadWalletBtn;//导入钱包按钮
@property (nonatomic, strong) NSMutableArray *walletArray;//数据源

@end

@implementation WKWalletManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    [self getWalletListNetworking];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"钱包管理";
    [self.tableView registerNib:[UINib nibWithNibName:@"WKWalletManagerTableViewCell" bundle:nil] forCellReuseIdentifier:@"WalletManager"];
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.foundWalletBtn];
    [self.bottomView addSubview:self.loadWalletBtn];
}

//获取钱包列表
- (void)getWalletListNetworking {
    [httpManager getWalletListWithPageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        NSDictionary *data = [result objectForKey:@"data"];
        NSDictionary *wallets = [data objectForKey:@"wallets"];
        for (NSDictionary *dict in wallets) {
            WKWalletModel *list = [WKWalletModel mj_objectWithKeyValues:dict];
            [self.walletArray addObject:list];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 145;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKWalletManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WalletManager" forIndexPath:indexPath];
    WKWalletModel *walletModel = self.walletArray[indexPath.row];
    cell.walletModel = walletModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WKMyWalletViewController *myWallet = [[WKMyWalletViewController alloc] init];
    myWallet.walletModel = self.walletArray[indexPath.row];
    [self.navigationController pushViewController:myWallet animated:YES];
}

#pragma mark - Button点击事件
- (void)clickFoundWalletBtn:(UIButton *)sender {
    WKFoundWalletViewController *foundWallet = [[WKFoundWalletViewController alloc] init];
    foundWallet.foundType = (int)sender.tag;
    [self.navigationController pushViewController:foundWallet animated:YES];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight-AppManager.tabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 20)];
        _tableView.backgroundColor = AppManager.tableBackColor;
    }
    return _tableView;
}

- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, AppManager.height-AppManager.tabBarHeight, AppManager.width, AppManager.tabBarHeight)];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (UIButton *)foundWalletBtn {
    if (_foundWalletBtn == nil) {
        _foundWalletBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _foundWalletBtn.frame = CGRectMake(0, 0, AppManager.width/2, AppManager.tabBarHeight);
        _foundWalletBtn.backgroundColor = [UIColor whiteColor];
        [_foundWalletBtn setTitle:@"创建钱包" forState:UIControlStateNormal];
        [_foundWalletBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _foundWalletBtn.titleLabel.font = AppManager.btnFont;
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, self.foundWalletBtn.frame.size.width, 0.5);
        layer.backgroundColor = [[UIColor grayColor] CGColor];
        [_foundWalletBtn.layer addSublayer:layer];
        _foundWalletBtn.tag = 1;
        [_foundWalletBtn addTarget:self action:@selector(clickFoundWalletBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _foundWalletBtn;
}

- (UIButton *)loadWalletBtn {
    if (_loadWalletBtn == nil) {
        _loadWalletBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _loadWalletBtn.frame = CGRectMake(AppManager.width/2, 0, AppManager.width/2, AppManager.tabBarHeight);
        [_loadWalletBtn setTitle:@"导入钱包" forState:UIControlStateNormal];
        [_loadWalletBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loadWalletBtn.titleLabel.font = AppManager.btnFont;
        _loadWalletBtn.backgroundColor = AppManager.themeColor;
        _loadWalletBtn.tag = 2;
        [_loadWalletBtn addTarget:self action:@selector(clickFoundWalletBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loadWalletBtn;
}

- (NSMutableArray *)walletArray {
    if (_walletArray == nil) {
        _walletArray = [[NSMutableArray alloc]init];
    }
    return _walletArray;
}

@end
